/**
 * 
 */
package com.fyp.ptma.model;

import java.io.Serializable;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.OneToMany;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

/**
 * @author YahyaArshad
 * 
 */
@Entity
@XmlRootElement(name = "version")
@XmlAccessorType(XmlAccessType.NONE)
public class ApplicationVersion implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long id;

	@XmlElement
	private boolean isUninstallPrevious = false;
	//private Set<Installation> installations = new HashSet<Installation>();

	public boolean isUninstallPrevious() {
		return isUninstallPrevious;
	}

	public void setUninstallPrevious(boolean isUninstallPrevious) {
		this.isUninstallPrevious = isUninstallPrevious;
	}

	@XmlElement
	private String versionComment;
	@XmlElementWrapper(name = "defects")
	@XmlElement(name = "defect")
	private List<Defect> defects = new ArrayList<Defect>();

	@XmlElement
	private ApplicationVersionPk versionPk;

	public ApplicationVersion() {

	}

	public ApplicationVersion(Long id, ApplicationVersionPk versionPk) {
		this.versionPk = versionPk;
		this.id = id;
	}

	@EmbeddedId
	public ApplicationVersionPk getVersionPk() {
		return versionPk;
	}

	public void setVersionPk(ApplicationVersionPk versionPk) {
		this.versionPk = versionPk;
	}

	@OneToMany(mappedBy = "appVersion")
	@Cascade(value = { CascadeType.ALL })
	@LazyCollection(LazyCollectionOption.FALSE)
	public List<Defect> getDefects() {
		return defects;
	}

	// @OneToMany(fetch = FetchType.LAZY, mappedBy = "downloader")
	// @Cascade(value = { CascadeType.SAVE_UPDATE })
	// public Set<Installation> getInstallations() {
	// return installations;
	// }
	//
	// public void setInstallations(Set<Installation> installations) {
	// this.installations = installations;
	// }

	public void setDefects(List<Defect> defects) {
		this.defects = defects;
	}

	private Date date = new Date(System.currentTimeMillis());

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(nullable = false, updatable = false, unique = true)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getVersionComment() {
		return versionComment;
	}

	public void setVersionComment(String versionComment) {
		this.versionComment = versionComment;
	}

	@Override
	public int hashCode() {
		if (this.getVersionPk() == null)
			return 0;
		return (this.getVersionPk().getVersion().hashCode());
	}

	@Override
	public boolean equals(Object obj) {

		if (obj == null || !(obj instanceof ApplicationVersion))
			return false;
		ApplicationVersion appVersion = (ApplicationVersion) obj;

		return appVersion.getVersionPk().getVersion()
				.equals(this.getVersionPk().getVersion())
				&& this.getVersionPk()
						.getApplication()
						.getNameSpace()
						.equals(this.getVersionPk().getApplication()
								.getNameSpace());
	}
}
